﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using ERP_RORZE_DLL.Product.DAO;
using ERP_RORZE_PERMISSION.Base;
using ERP_RORZE_PRODUCT.Product.database;
using ERP_RORZE_DLL.Product.database;
using ERP_RORZE_DLL.Product.Base;

namespace ERP_RORZE_PRODUCT.Product.Machine
{
    public partial class ctrlMachineConfiguration : DevExpress.XtraEditors.XtraUserControl
    {
        ClsMachineDAO clsmachineDAO = new ClsMachineDAO();
        DataTable dt;
        private LogUtils log;
        ClsDataSheetTable b = new ClsDataSheetTable();
        public ctrlMachineConfiguration()
        {
            InitializeComponent();
            log = new LogUtils();
        }

        void Reload(object sender, EventArgs e)
        {
            dt = clsmachineDAO.GetAllMachineConfiguration();
            dgvMachineconfig.DataSource = dt;
            gridView1.BestFitColumns();
        }
        private void ctrlMachineConfiguration_Load(object sender, EventArgs e)
        {
            Reload(sender, e);
        }

        private void btnRefresh_Click(object sender, EventArgs e)
        {
            Reload(sender, e);
        }

        private void btnReload_Click(object sender, EventArgs e)
        {
            Reload(sender, e);
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            frm_MachineConfigAdd frmAdd = new frm_MachineConfigAdd();
            frmAdd.Reload += new EventHandler(Reload);
            frmAdd.ShowDialog();
        }

        private void btnAddNew_Click(object sender, EventArgs e)
        {
            btnAdd_Click(sender, e);
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            b.ID = int.Parse(gridView1.GetRowCellValue(gridView1.FocusedRowHandle,"ID").ToString());
            clsmachineDAO.DeleteMachineConfiguration(b);

            //write Log
            log.Table = LogConstant.Table.Product.Datasheet;
            log.Task = LogConstant.Task.Delete;
            log.Description = "ID: " + b.ID;
            log.WriteLog();

            Reload(sender, e);
        }

        private void btDelete_Click(object sender, EventArgs e)
        {
            btnDelete_Click(sender, e);
        }

        private void btEdit_Click(object sender, EventArgs e)
        {
            try
            {
                frm_MachineConfigEdit frmEdit = new frm_MachineConfigEdit();
                frmEdit.Reload += new EventHandler(Reload);
                frmEdit.ID = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "ID").ToString();
                frmEdit.Show();
            }
            catch(Exception e1)
            {
                MessageBox.Show(""+e1);
            }
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            btEdit_Click(sender, e);
        }

        private void btnExport_Click(object sender, EventArgs e)
        {
            clsConstantsFuction.ExportToExcel(gridView1, 1, true);
        }
    }
}
